package cn.virens.common.project.generator.enums;


import cn.virens.common.mybatis.entity.BaseEnum;
import cn.virens.common.util.exception.APIException;
import org.dromara.hutool.core.reflect.method.MethodUtil;
import org.dromara.hutool.core.text.StrUtil;

import java.lang.reflect.Method;

public class EnumScanToJsonUtil {
    public static void generator(String... pkgs) throws APIException {
        StringBuilder builder = new StringBuilder("");

        EnumScan.scanPackage(pkgs).forEach((class1) -> {
            String name = StrUtil.toUnderlineCase(class1.getSimpleName());
            Method method = MethodUtil.getMethodByName(class1, "values");

            BaseEnum<?>[] enums = MethodUtil.invokeStatic(method);

            builder.append("export const ").append(name).append(" = [\r\n");

            for (BaseEnum<?> enum1 : enums) {
                builder.append("  [\"");
                builder.append(enum1.getKey()).append("\", \"");
                builder.append(enum1.getDesc()).append("\"],\r\n");
            }

            builder.append("];\r\n\r\n");
        });

        System.out.println(builder);
    }
}